package com.zhou.app.favourite;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;

import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * https://blog.csdn.net/weixin_44795847/article/details/123702038
 * @author xianfeng
 * @date 2023/5/14 20:50
 */
public class GuavaDemo {
    public static void main(String args[]) throws Exception {
        LoadingCache<String, Object> cache = CacheBuilder.newBuilder()
                // 最大3个 //Cache中存储的对象,写入3秒后过期
                .maximumSize(3).expireAfterWrite(3, TimeUnit.SECONDS)
                .recordStats().removalListener(new
                                                       RemovalListener<Object, Object>() {
                                                           public void onRemoval(RemovalNotification<Object, Object>
                                                                                         notification) {
                                                               System.out.println(notification.getKey() + ":" + notification.getCause());
                                                           }
                                                       })
                .build(
                        new CacheLoader<String, Object>() {
                            @Override
                            public String load(String s) throws Exception {
                                //todo 从数据库或redis加载
                                return "find in database or redis";
                            }
                        }
                );
        /*
        初始化cache
        */
        initCache(cache);
        System.out.println(cache.size());
        displayCache(cache);
        System.out.println("=============================");
        Thread.sleep(1000);
        System.out.println(cache.getIfPresent("1"));
        Thread.sleep(2500);
        System.out.println("=============================");
        displayCache(cache);
    }

    public static Object get(String key, LoadingCache cache) throws Exception {
        Object value = cache.get(key, new Callable() {
            @Override
            public Object call() throws Exception {
                String v = "find in database or redis";
                //设置回缓存
                cache.put(key, v);
                return v;
            }
        });
        return value;
    }

    public static void initCache(LoadingCache cache) throws Exception {
        /*
        前三条记录
        */
        for (int i = 1; i <= 3; i++) {
            cache.get(String.valueOf(i));
        }
    }

    /**
     *获得当前缓存的记录
     @param
     cache
     *@throws Exception
     **/

    public static void displayCache(LoadingCache cache) throws Exception {
        Iterator its = cache.asMap().entrySet().iterator();
        while (its.hasNext()) {
            System.out.println(its.next().toString());
        }
    }
}
